package com.how2java.tmall.web;

import com.how2java.tmall.pojo.Product;
import com.how2java.tmall.service.ProductService;
import com.how2java.tmall.util.Page4Navigator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/categories/{cid}/products")
    public Page4Navigator<Product> list(@PathVariable("cid") int cid,
                                        @RequestParam(value="start",defaultValue = "0") int start,
                                        @RequestParam(value="size",defaultValue = "5") int size){
        start = start<0?0:start;
        Page4Navigator<Product> page = productService.list(cid,start,size,5);
        return page;
    }

    @GetMapping("/products/{id}")
    public Product get(@PathVariable("id") int id){
        Product bean = productService.get(id);
        return bean;
    }

    @PostMapping("/products")
    public Object add(@RequestBody Product bean){
        productService.add(bean);
        return bean;
    }

    @DeleteMapping("/products/{id}")
    public String delete(@PathVariable("id")int id){
        productService.delete(id);
        return null;
    }

    @PutMapping("/products")
    public Object update(@RequestBody Product bean){
        productService.update(bean);
        return bean;
    }





}
